capture program drop construct_base_4duration
program define construct_base_4duration
* Arguments
* 1: input
* 2: output
* 3 4: right and left censoring
* 5: either whole for whole sample or sub for sub-periods, the difference being that 
* 6: country group to be considered

clear 
g $product=0 
drop if $product==0 
save `2', replace 

use `1', clear
drop if $product==0
g sample=0
foreach i in `6' {
	replace sample=1 if iso2=="`i'"
}
keep if sample==1
sort year month	
id_group id_t "year month" // id_group does the same job as egen group (but work if we have millions of groups)
sort iso2 buyer $product year month siren
id_group id_sc "siren iso2" // id_group does the same job as egen group (but work if we have millions of groups)
id_group id_b "iso2 buyer" // id_group does the same job as egen group (but work if we have millions of groups)
id_group id_bp "iso2 buyer $product" // id_group does the same job as egen group (but work if we have millions of groups)
id_group id_bst "id_bp id_t id_s" // id_group does the same job as egen group (but work if we have millions of groups)
id_group id_bs "id_bp id_s"	 // id_group does the same job as egen group (but work if we have millions of groups)
sort id_bp id_t
id_group _ "id_bp id_t"
by id_bp: egen __=min(_)
g id_t_bp=_-__+1
drop _ __
sort id_sc id_bp id_t_bp
g switch=0
by id_sc id_bp: replace switch=1 if _n==1
* switch =1 the first time the seller and the buyer ever interact (including the first time the buyer appears in the sample) 
sort id_sc id_bp id_t_bp
g recall=0
replace recall=1 if id_t_bp!=id_t_bp[_n-1]+1 & id_sc==id_sc[_n-1] & id_bp==id_bp[_n-1] 
* recall=1 for a switch towards a seller the buyer had already met
sort id_bp id_sc id_t_bp
by id_bp id_sc: g _=sum(recall)
id_group id_bs_continuous "id_bp id_sc _"
*  id_bs_continuous identifies seller buyer continuous relationships (ie recalls are attributed a different id)
drop _
* length of the buyer's history
sort id_bp id_t
by id_bp: g first=1 if _n==1
by id_bp: g last=1 if _n==_N
g date=year+(month-1)/12
g _=date*first
by id_bp: egen datefirst=mean(_)
drop _
g _=date*last	
by id_bp: egen datelast=mean(_)
drop _
g length_bp=(datelast-datefirst)*12+1
g datelast_bp=datelast
drop datelast datefirst first last
// length_bp defined as the difference between the first and the last months of a buyer's history
* length of a continuous relationship 
sort id_bs_continuous id_t
by id_bs_continuous: g first=1 if _n==1
by id_bs_continuous: g last=1 if _n==_N
g _=date*first
by id_bs_continuous: egen datefirst=mean(_)
drop _
g _=date*last	
by id_bs_continuous: egen datelast=mean(_)
drop _
g length_bps=(datelast-datefirst)*12+1
// length_bps defined as the difference between the first and the last months of a continuous relationship
g firstyear_panel=substr(firstdate,1,4)
destring firstyear_panel, replace
g firstyear_sample=floor(datefirst)
g right_censored=0
`3'	
g left_censored=0
`4' // drop all seller*buyer relationships that we do not observe from the start (treats left-censoring)
drop datelast datefirst first last firstyear_panel firstyear_sample

compress
keep length_bp length_bps export right_censored left_censored id_bs_continuous id_bs id_bp $product iso2

collapse (mean) length_bp length_bps export right_censored left_censored (sum) totx=export, by(id_bs_continuous id_bs id_bp $product iso2)
* collapse across transactions within a (continuous) buyer* seller* $product
save `2', replace 
erase temp.dta 
end
